import axios from "axios";

export const BL_ImageGenSize = {
    rate1_1: "1328*1328",
    rate3_4: "1140*1472",
    rate4_3: "1472*1140",
    rate16_9: "1664*928",
    rate9_16: "928*1664",
    rate2_3: "1140*1472",
    rate3_2: "1472*1140",
    rate21_9: "1664*928"
}

export class BaiLianService {
    private appKey = "sk-8bc796c0918740f1a07a9f736b1b47ae";
    /**
      *
      *
      * @param {string} input
      * @param {ImageGenSize} size
      * @param {string} inputImage 传入图像就是图像编辑，只能传入一张
      * @param {string} [negative_prompt=""]
      * @returns
      * @memberof BaiLianService
      */
    async GenOrEidtImage(input: string, size: string, inputImage: string, negative_prompt: string = "") {
        const url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation";
        let data = {
            "model": "qwen-image",
            "input": {
                "messages": [
                    {
                        "role": "user",
                        "content": []
                    }
                ]
            },
            "parameters": {
                "negative_prompt": negative_prompt,
                "prompt_extend": false,
                "watermark": false,
                "size": BL_ImageGenSize[size]
            }
        };

        if (inputImage) {
            data.model = "qwen-image-edit";
            data.input.messages[0].content.push({
                image: inputImage
            });
        }
        data.input.messages[0].content.push({ text: input });

        const result = await axios({
            method: 'POST',
            url,
            data,
            headers: {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + this.appKey
            }
        });
        const newr = result.data.output.choices[0].message.content.map(x => { return { url: x.image } });
        return {
            content: JSON.stringify(newr)
        };
    }
}